package com.jsmtr.rms.service.impl;

import com.jsmtr.rms.dao.DaoService;
import com.jsmtr.rms.entity.ProjectInfoJobtemplate;
import com.jsmtr.rms.service.IProjectInfoJobTemplateService;
import com.jsmtr.rms.utils.BeanTools;
import com.jsmtr.rms.utils.db.DbUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: cyx
 * @description:
 * @date: Created in 2018/9/3 17:34
 * @modified by:
 */
@Service
@Transactional
public class ProjectInfoJobTemplateService  implements IProjectInfoJobTemplateService{
    @Resource
    private DaoService daoService;

    public DaoService getDaoService() {
        return this.daoService;
    }

    public void setDaoService(DaoService daoService) {
        this.daoService = daoService;
    }

    @Override
    public  Map<String, Object> findProjectInfoJobTemplateByPage(int page,int perpage,ProjectInfoJobtemplate jobtemplate) throws  Exception{
        StringBuffer queryHQL = new StringBuffer("from ProjectInfoJobtemplate p where 1=1 ");
        StringBuffer countHQL = new StringBuffer("select count(*) from ProjectInfoJobtemplate p where 1=1 ");
        StringBuffer condition = new StringBuffer();
        condition.append(BeanTools.hqlWhere(jobtemplate, "p"));
        queryHQL.append(condition).append(" order by p.createtime desc");
        countHQL.append(condition);
        return DbUtil.pageQuery(page, perpage, queryHQL.toString(), countHQL.toString(), daoService);

    }

    @Override
    public ProjectInfoJobtemplate getPorjectJobTemplate(String tempid) throws Exception {

        return (ProjectInfoJobtemplate) daoService.get(ProjectInfoJobtemplate.class,tempid);
    }

    @Override
    public List<ProjectInfoJobtemplate> findProjectInfoJobTemplateByLike(String state, String jobName, String tempName, String type) throws Exception {
        String findHql=" from ProjectInfoJobtemplate where 1=1 ";
        Map<String,Object> params=new HashMap<>();
        if(state!=null&&state.length()>0) {
            findHql += " and state=:state ";
            params.put("state", state);
        }
        if(jobName!=null&&jobName.length()>0){
            findHql+=" and jobname like '%'||:jobname||'%'";
            params.put("jobname",jobName);
        }
        if(tempName!=null&&tempName.length()>0){
            findHql+=" and tempname like '%'||:tempname||'%'";
            params.put("tempname",tempName);
        }

        if(type!=null&&type.length()>0){
            findHql+=" and type = :type";
            params.put("type",type);
        }
        if (params.size()>0){
            return  daoService.find(findHql,params);
        }else{
            return  daoService.find(findHql);
        }
    }


    @Override
    public List<ArrayList> findPlans_0598(String[] id) throws Exception {
        return daoService.findSql("select * from formmain_0598 where id in(:idlist)", id);
    }

    @Override
    public List<ArrayList> findColumnsPlans(String name) throws Exception {
        return daoService.findSql("select * from user_tab_columns where Table_Name='"+name+"'");
    }

    @Override
    public List<ArrayList> findPlansALL_0598() throws Exception {
        return daoService.findSql("select * from FORMMAIN_0598");
    }

    @Override
    public List<ArrayList> findPlans_0599(String[] id) throws Exception {
        return daoService.findSqlS("select * from FORMMAIN_0599 where formmain_id in(:idlist) ", id);
    }

    @Override
    public List<ProjectInfoJobtemplate> findAllProjectJobTemplate() throws Exception {
        String findHql=" from ProjectInfoJobtemplate  ";
        return daoService.find(findHql);
    }

    @Override
    @Transactional
    public void addProjectJobTemplate(ProjectInfoJobtemplate jobtemplate) throws Exception {

        daoService.add(jobtemplate);
    }

    @Override
    @Transactional
    public void editProjectJobTemplate(ProjectInfoJobtemplate jobtemplate) throws Exception {
        daoService.update(jobtemplate);
    }

    @Override
    @Transactional
    public void deleteProjectJobTemplate(ProjectInfoJobtemplate jobtemplate) throws Exception {
        daoService.delete(jobtemplate);
    }
}
